package com.ysy.tz.mfg.equipment.scale;

import com.ysy.tz.mfg.equipment.CommEquipment;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class YaoHuaScale extends CommEquipment {
    private static final Logger log = LoggerFactory.getLogger(YaoHuaScale.class);
    int[] dataInput = new int[60];
    int inputIndex = 0;
    private int digit = 10000;

    public YaoHuaScale() {
        this.setDataBits(8);
        this.setStopBits(1);
        this.setParity(0);
    }

    public YaoHuaScale(String portName, int baudrate, int fractionDigits) {
        super(portName, baudrate, fractionDigits);
        this.setDataBits(8);
        this.setStopBits(1);
        this.setParity(0);
    }

    protected void messageReceived(InputStream inputStream) throws Exception {
        while(true) {
            try {
                if (inputStream.available() > 0) {
                    int inputInt = inputStream.read();
                    char c = (char)inputInt;
                    if (c == '+') {
                        this.inputIndex = 0;
                        StringBuffer sb = new StringBuffer();

                        for(int i = 0; i <= 7; ++i) {
                            sb.append((char)this.dataInput[i]);
                        }

                        try {
                            double d = Double.parseDouble(sb.toString());
                            d /= (double)this.digit * Math.pow(10.0, (double)this.getFractionDigits());
                            this.setCurrentWeight(new BigDecimal(d));
                        } catch (Exception var7) {
                        }
                    } else {
                        if (this.inputIndex != 60) {
                            this.dataInput[this.inputIndex] = inputInt;
                            ++this.inputIndex;
                            continue;
                        }

                        this.inputIndex = 0;
                    }
                }
            } catch (IOException var8) {
                log.error("称解析失败!", var8);
            }

            return;
        }
    }

    public void setDigit(int digit) {
        this.digit = digit;
    }

    public static void main(String[] args) {
        YaoHuaScale scale = new YaoHuaScale();
        scale.setFractionDigits(5);
        System.out.println(scale.getDecimalFormat().getRoundingMode());
    }
}
